#define CommWifi_Variable

#include "userDef.h"



stat_wifi_typeDef stat_wifi;
timer_wifi_typeDef timer_wifi;
buff_wifi_typeDef buff_wifi;



void Comm_wifi_Tx(U8 *tx_buf, U8 tx_num) /* Wi-Fi通讯发送 */
{
    f_wifi_txd_ing = ON;

    Comm_wifi.tx_address = tx_buf;
    Comm_wifi.tx_count = tx_num;

    Comm_wifi_Tx_Intp();
}

void Comm_wifi_Tx_Intp(void) /* Wi-Fi通讯发送中断 */
{
    if (Comm_wifi.tx_count > 0)
    {
        SCI0->TXD0 = *Comm_wifi.tx_address;
        Comm_wifi.tx_address++;
        Comm_wifi.tx_count--;
    }
    else
    {
        f_wifi_txd_ing = OFF;
    }
}

void Comm_wifi_Rx(U8 *rx_buf, U8 rx_num) /* Wi-Fi通讯接收 */
{
    Comm_wifi.rx_count = 0U;
    Comm_wifi.rx_length = rx_num;
    Comm_wifi.rx_address = rx_buf;
}

void Comm_wifi_Rx_Intp(U8 data) /* Wi-Fi通讯接收中断 */
{
    if (Comm_wifi.rx_length > Comm_wifi.rx_count)
    {
        *Comm_wifi.rx_address = data;
        Comm_wifi.rx_address++;
        Comm_wifi.rx_count++;
    }

    if (Comm_wifi.rx_count >= 7)
    {
        if (buff_wifi.rxd[WIFI_HEADER0_ADDR] == WIFI_HEADER0_BYTE && buff_wifi.rxd[WIFI_HEADER1_ADDR] == WIFI_HEADER1_BYTE)
        {
            if (Comm_wifi.rx_count >= (buff_wifi.rxd[WIFI_LENGTH1_ADDR] + 7) || Comm_wifi.rx_count >= WIFI_RXD_LENGTH)
            {
                f_wifi_rxd_end = TRUE;
            }
        }
        else
        {
            f_wifi_rxd_end = FALSE;

            memset(buff_wifi.rxd, 0, sizeof(buff_wifi.rxd));

            Comm_wifi_Rx(buff_wifi.rxd, WIFI_RXD_LENGTH);
        }
    }

    timer_wifi.rxd = 10; /* 0.5ms * 10 = 5ms */
}

U8 get_check_sum(U8 *pack, U8 pack_len)
{
    U8 i;
    static U8 check_sum = 0;
    check_sum = 0;
    for (i = 0; i < pack_len; i++)
    {
        check_sum += pack[i];
    }

    return check_sum;
}

void CommWifiAct(void) /* called in main.c */
{
    if (timer_wifi.rxd == 0 && Comm_wifi.rx_count != 0)
    {
        f_wifi_rxd_end = FALSE;

        memset(buff_wifi.rxd, 0, sizeof(buff_wifi.rxd));

        Comm_wifi_Rx(buff_wifi.rxd, WIFI_RXD_LENGTH);
    }

    Comm_wifi_data_down(); /* 下行数据处理 */

    if (f_wifi_txd_ing == OFF)
    {
        if (f_wifi_heart == OFF)
        {
            if (f_wifi_reply_heart == ON)
            {
                CLR(f_wifi_reply_heart);

                Comm_wifi_reply_heart(); /* 应答心跳检测 */
            }
        }
        else if (f_wifi_reply_heart == ON)
        {
            CLR(f_wifi_reply_heart);

            Comm_wifi_reply_heart(); /* 应答心跳检测 */
        }
        else if (f_wifi_reply_device_info == ON)
        {
            CLR(f_wifi_reply_device_info);

            Comm_wifi_reply_device_info(); /* 应答设备信息 */
        }
        else if (f_wifi_reply_model_mode == ON)
        {
            CLR(f_wifi_reply_model_mode);

            Comm_wifi_reply_model_mode(); /* 应答模块工作方式 */
        }
        else if (f_wifi_reply_model_stat == ON)
        {
            CLR(f_wifi_reply_model_stat);

            Comm_wifi_reply_model_stat(); /* 应答模块工作状态 */
        }
        else if (f_wifi_test == ON)
        {
            CLR(f_wifi_test);

            Comm_wifi_to_test(); /* wifi模块进入产测 */
        }
        else if (f_wifi_rst == ON)
        {
            CLR(f_wifi_rst);

            Comm_wifi_to_reset(); /* wifi模块进入重置 */
        }
        else
        {
            Comm_wifi_data_up(); /* 上行数据处理 */
        }
    }
}

void Comm_wifi_data_down(void) /* 接收数据处理 */
{
    if (f_wifi_rxd_end == TRUE)
    {
        if (get_check_sum(buff_wifi.rxd, buff_wifi.rxd[WIFI_LENGTH1_ADDR] + 6) == buff_wifi.rxd[buff_wifi.rxd[WIFI_LENGTH1_ADDR] + 6])
        {
            switch (buff_wifi.rxd[WIFI_COMMAND_ADDR])
            {
            case 0x00: /* 心跳检测 */
                SET(f_wifi_reply_heart);
                break;

            case 0x01: /* 设备信息 */
                SET(f_wifi_reply_device_info);
                break;

            case 0x02: /* 模块工作方式-MCU与模块配合处理 */
                SET(f_wifi_reply_model_mode);
                break;

            case 0x03: /* 模块工作状态 */
                if (stat_wifi.net != buff_wifi.rxd[WIFI_DATA_ADDR])
                {
                    stat_wifi.net = buff_wifi.rxd[WIFI_DATA_ADDR];

                    if (stat_wifi.net == 0x04)
                    {
                        device_data_upd_all1 = 0xFF;
                        device_data_upd_all2 = 0xFF;
                        device_data_upd_all3 = 0xFF;
                        device_data_upd_all4 = 0xFF;
                        device_data_upd_all_5s = 0xFF;
                        device_data_upd_all_3s = 0xFF;
                        device_data_upd_all_2s = 0xFF;

                        timer_wifi.upd_5s = 50;
                        timer_wifi.upd_5s = 30;
                        timer_wifi.upd_5s = 20;
                    }
                }

                SET(f_wifi_reply_model_stat);
                break;

            case 0x04: /* 重置wifi */
            case 0x05: /* 重置wifi选择模式 */
                break;

            case 0x06:                 /* 下发控制指令 */
                Comm_wifi_down_ctrl(); /* 下发控制指令 */
                break;

            case 0x08: /* 查询设备工作状态 */
                device_data_upd_all1 = 0xFF;
                device_data_upd_all2 = 0xFF;
                device_data_upd_all3 = 0xFF;
                device_data_upd_all4 = 0xFF;
                device_data_upd_all_5s = 0xFF;
                device_data_upd_all_3s = 0xFF;
                device_data_upd_all_2s = 0xFF;

                timer_wifi.upd_5s = 50;
                timer_wifi.upd_5s = 30;
                timer_wifi.upd_5s = 20;
                break;

            case 0x0A: /* 升级启动 */
                timer_wifi.ota_delay = 40;
                break;

            case 0x0B: /* 升级包传输 */
                break;

            case 0x0E: /* 产测结果 */
                if (buff_wifi.rxd[WIFI_DATA_ADDR] == 0x01)
                {
                    f_code_o3 = ON;

                    f_wifi_test_ok = ON;

                    BuzzerAct(BUZZER400ms);
                }
                break;

            default:
                break;
            }
        }

        f_wifi_rxd_end = FALSE;

        memset(buff_wifi.rxd, 0, sizeof(buff_wifi.rxd));

        Comm_wifi_Rx(buff_wifi.rxd, WIFI_RXD_LENGTH);
    }
}

void Comm_wifi_reply_heart(void) /* 应答心跳检测 */
{
    memset(buff_wifi.txd, 0, sizeof(buff_wifi.txd));

    buff_wifi.txd[WIFI_HEADER0_ADDR] = WIFI_HEADER0_BYTE;
    buff_wifi.txd[WIFI_HEADER1_ADDR] = WIFI_HEADER1_BYTE;

    buff_wifi.txd[WIFI_VERSION_ADDR] = WIFI_VERSION_BYTE;
    buff_wifi.txd[WIFI_COMMAND_ADDR] = 0x00;

    buff_wifi.txd[WIFI_LENGTH0_ADDR] = 0x00;
    buff_wifi.txd[WIFI_LENGTH1_ADDR] = 0x01;

    if (f_wifi_heart == OFF)
    {
        f_wifi_heart = ON;

        buff_wifi.txd[WIFI_DATA_ADDR] = 0x00;
    }
    else
    {
        buff_wifi.txd[WIFI_DATA_ADDR] = 0x01;
    }

    buff_wifi.txd[7] = get_check_sum(buff_wifi.txd, 7);

    Comm_wifi_Tx(buff_wifi.txd, 8);
}

void Comm_wifi_reply_device_info(void) /* 应答设备信息 */
{
    U8 i;

    memset(buff_wifi.txd, 0, sizeof(buff_wifi.txd));

    buff_wifi.txd[WIFI_HEADER0_ADDR] = WIFI_HEADER0_BYTE;
    buff_wifi.txd[WIFI_HEADER1_ADDR] = WIFI_HEADER1_BYTE;

    buff_wifi.txd[WIFI_VERSION_ADDR] = WIFI_VERSION_BYTE;
    buff_wifi.txd[WIFI_COMMAND_ADDR] = 0x01;

    buff_wifi.txd[WIFI_LENGTH0_ADDR] = 0x00;
    buff_wifi.txd[WIFI_LENGTH1_ADDR] = 0x2A;

    for (i = 0; i < 24; i++)
    {
        buff_wifi.txd[WIFI_DATA_ADDR + i] = DEVICE_ID[i];
    }

    for (i = 0; i < 12; i++)
    {
        buff_wifi.txd[WIFI_DATA_ADDR + 24 + i] = DEVICE_VERSION[i];
    }

    for (i = 0; i < 6; i++)
    {
        buff_wifi.txd[WIFI_DATA_ADDR + 36 + i] = DEVICE_NET[i];
    }

    buff_wifi.txd[48] = get_check_sum(buff_wifi.txd, 48);

    Comm_wifi_Tx(buff_wifi.txd, 49);
}

void Comm_wifi_reply_model_mode(void) /* 应答模块工作方式 */
{
    memset(buff_wifi.txd, 0, sizeof(buff_wifi.txd));

    buff_wifi.txd[WIFI_HEADER0_ADDR] = WIFI_HEADER0_BYTE;
    buff_wifi.txd[WIFI_HEADER1_ADDR] = WIFI_HEADER1_BYTE;

    buff_wifi.txd[WIFI_VERSION_ADDR] = WIFI_VERSION_BYTE;
    buff_wifi.txd[WIFI_COMMAND_ADDR] = 0x02;

    buff_wifi.txd[WIFI_LENGTH0_ADDR] = 0x00;
    buff_wifi.txd[WIFI_LENGTH1_ADDR] = 0x00;

    buff_wifi.txd[6] = get_check_sum(buff_wifi.txd, 6);

    Comm_wifi_Tx(buff_wifi.txd, 7);
}

void Comm_wifi_reply_model_stat(void) /* 应答模块工作状态 */
{
    memset(buff_wifi.txd, 0, sizeof(buff_wifi.txd));

    buff_wifi.txd[WIFI_HEADER0_ADDR] = WIFI_HEADER0_BYTE;
    buff_wifi.txd[WIFI_HEADER1_ADDR] = WIFI_HEADER1_BYTE;

    buff_wifi.txd[WIFI_VERSION_ADDR] = WIFI_VERSION_BYTE;
    buff_wifi.txd[WIFI_COMMAND_ADDR] = 0x03;

    buff_wifi.txd[WIFI_LENGTH0_ADDR] = 0x00;
    buff_wifi.txd[WIFI_LENGTH1_ADDR] = 0x00;

    buff_wifi.txd[6] = get_check_sum(buff_wifi.txd, 6);

    Comm_wifi_Tx(buff_wifi.txd, 7);
}

void Comm_wifi_to_test(void) /* wifi模块进入产测 */
{
    memset(buff_wifi.txd, 0, sizeof(buff_wifi.txd));

    buff_wifi.txd[WIFI_HEADER0_ADDR] = WIFI_HEADER0_BYTE;
    buff_wifi.txd[WIFI_HEADER1_ADDR] = WIFI_HEADER1_BYTE;

    buff_wifi.txd[WIFI_VERSION_ADDR] = WIFI_VERSION_BYTE;
    buff_wifi.txd[WIFI_COMMAND_ADDR] = 0x0E;

    buff_wifi.txd[WIFI_LENGTH0_ADDR] = 0x00;
    buff_wifi.txd[WIFI_LENGTH1_ADDR] = 0x00;

    buff_wifi.txd[6] = get_check_sum(buff_wifi.txd, 6);

    Comm_wifi_Tx(buff_wifi.txd, 7);
}

void Comm_wifi_to_reset(void) /* wifi模块进入重置 */
{
    memset(buff_wifi.txd, 0, sizeof(buff_wifi.txd));

    buff_wifi.txd[WIFI_HEADER0_ADDR] = WIFI_HEADER0_BYTE;
    buff_wifi.txd[WIFI_HEADER1_ADDR] = WIFI_HEADER1_BYTE;

    buff_wifi.txd[WIFI_VERSION_ADDR] = WIFI_VERSION_BYTE;
    buff_wifi.txd[WIFI_COMMAND_ADDR] = 0x04;

    buff_wifi.txd[WIFI_LENGTH0_ADDR] = 0x00;
    buff_wifi.txd[WIFI_LENGTH1_ADDR] = 0x00;

    buff_wifi.txd[6] = get_check_sum(buff_wifi.txd, 6);

    Comm_wifi_Tx(buff_wifi.txd, 7);
}

void Comm_wifi_down_ctrl(void) /* 下发控制指令 */
{
    switch (buff_wifi.rxd[WIFI_DPID_ADDR])
    {
    case DPID_POWER: /* 0x01,电源开关 */
        set_power(buff_wifi.rxd[WIFI_ORDER_ADDR]);
        break;

    case DPID_TARTEMP_C: /* 0x02,目标温度C */
        set_tartemp((U16)buff_wifi.rxd[WIFI_ORDER_ADDR + 3] * 10, ((U16)(buff_wifi.rxd[WIFI_ORDER_ADDR + 3] * 10 * 1.8) + 320) / 10 * 10);
        break;

    case DPID_TARTEMP_F: /* 0x18,目标温度F */
        set_tartemp((U16)(buff_wifi.rxd[WIFI_ORDER_ADDR + 3] * 10 - 320) * 10 / 18 / 5 * 5, (U16)buff_wifi.rxd[WIFI_ORDER_ADDR + 3] * 10);
        break;

    case DPID_TEMPUNIT: /* 0x13,温标切换 */
        set_tempUnit(buff_wifi.rxd[WIFI_ORDER_ADDR]);
        break;

    case DPID_MODE: /* 0x04,模式 */
        set_mode(buff_wifi.rxd[WIFI_ORDER_ADDR]);
        break;

    case DPID_FANLEVEL: /* 0x05,风挡 */
        switch (buff_wifi.rxd[WIFI_ORDER_ADDR])
        {
        case 0x00:
            set_fanStep(FANAUTO);
            break;

        case 0x01:
            set_fanStep(FANLEVEL1);
            break;

        case 0x02:
            set_fanStep(FANLEVEL2);
            break;

        case 0x03:
            set_fanStep(FANLEVEL3);
            break;

        case 0x04:
            set_fanStep(FANLEVEL4);
            break;

        case 0x05:
            set_fanStep(FANLEVEL5);
            break;

        case 0x06:
            set_fanStep(FANLEVEL6);
            break;

        case 0x07:
            set_fanStep(FANLEVEL7);
            break;

        default:
            set_fanStep(FANAUTO);
            break;
        }
        break;

    case DPID_VERSWING: /* 0x1F,上下扫风 */
        set_verSw(buff_wifi.rxd[WIFI_ORDER_ADDR]);
        break;

    case DPID_HORSWING: /* 0x22,左右扫风 */
        set_horSw(buff_wifi.rxd[WIFI_ORDER_ADDR]);
        break;

    case DPID_ECO: /* 0x08,节能功能 */
        if (e_mode.set == MODECOOL)
        {
            set_eco(buff_wifi.rxd[WIFI_ORDER_ADDR]);
        }
        else
        {
            set_eco(OFF);
        }
        break;

    case DPID_DRYER: /* 0x09,干燥功能 */
        set_dryer(buff_wifi.rxd[WIFI_ORDER_ADDR]);
        break;

    case DPID_HEATER: /* 0x0C,辅热功能 */
        set_heater(buff_wifi.rxd[WIFI_ORDER_ADDR]);
        break;

    case DPID_LIGHT: /* 0x0D,灯光开关 */
        set_light(buff_wifi.rxd[WIFI_ORDER_ADDR]);
        break;

    case DPID_SLEEP: /* 0x19,睡眠功能 */
        set_sleep(buff_wifi.rxd[WIFI_ORDER_ADDR]);
        break;

    case DPID_HEALTH: /* 0x1A,健康功能 */
        set_health(buff_wifi.rxd[WIFI_ORDER_ADDR]);
        break;

    case DPID_CHILDLOCK: /* 0x0E,童锁功能 */
        set_childlock(buff_wifi.rxd[WIFI_ORDER_ADDR]);
        break;

    case DPID_BUZZER: /* 0x10,蜂鸣器功能 */
        set_buzzer(buff_wifi.rxd[WIFI_ORDER_ADDR]);
        break;

    case DPID_HUMIDIFIER: /* 0x0B,加湿器功能 */
        set_humidifier(buff_wifi.rxd[WIFI_ORDER_ADDR]);
        break;

    case DPID_TARHUM: /* 0x0B,设定湿度功能 */
        set_tarHum((U16)buff_wifi.rxd[WIFI_ORDER_ADDR + 3] * 10);
        break;
    /*
    case DPID_TIMER: // 0x14,定时
        if (b_power.set == OFF)
        {
            set_timerOn(buff_wifi.rxd[WIFI_ORDER_ADDR] * 60);
        }
        else
        {
            set_timerOff(buff_wifi.rxd[WIFI_ORDER_ADDR] * 60);
        }
        break;
    */
    case DPID_SET_CAP_TEST: /* 0xAA，能力测试设定 */
        set_captest(buff_wifi.rxd[WIFI_ORDER_ADDR]);
        break;

    case DPID_SET_EXP_STEP: /* 0xAB，电子膨胀阀开度设定 */
        set_exp_step(((U16)buff_wifi.rxd[WIFI_ORDER_ADDR + 2] << 8) + buff_wifi.rxd[WIFI_ORDER_ADDR + 3]);
        break;

    case DPID_SET_EXH_TEMP: /* 0xAC，排气温度设定 */
        set_exh_temp(((U16)buff_wifi.rxd[WIFI_ORDER_ADDR + 2] << 8) + buff_wifi.rxd[WIFI_ORDER_ADDR + 3]);
        break;

    case DPID_SET_COMP_FREQ: /* 0xAD，压缩机频率设定 */
        set_comp_freq(((U16)buff_wifi.rxd[WIFI_ORDER_ADDR + 2] << 8) + buff_wifi.rxd[WIFI_ORDER_ADDR + 3]);
        break;

    case DPID_SET_OUT_FAN: /* 0xAE，外风机转速/档位设定 */
        set_out_fan(((U16)buff_wifi.rxd[WIFI_ORDER_ADDR + 2] << 8) + buff_wifi.rxd[WIFI_ORDER_ADDR + 3]);
        break;

    case DPID_SET_IN_FAN: /* 0xAF，内风机转速/档位设定 */
        set_in_fan(((U16)buff_wifi.rxd[WIFI_ORDER_ADDR + 2] << 8) + buff_wifi.rxd[WIFI_ORDER_ADDR + 3]);
        break;

    case DPID_SET_SW_STEP: /* 0xB0，扫风角度设定 */
        set_sw_step(((U16)buff_wifi.rxd[WIFI_ORDER_ADDR + 2] << 8) + buff_wifi.rxd[WIFI_ORDER_ADDR + 3]);
        break;

    case DPID_SET_CORRECT: /* 0xB1，补偿温度设定 */
        set_correct(buff_wifi.rxd[WIFI_ORDER_ADDR]);
        break;

    case DPID_APP_SET_TEST: /* 0xC6，app设置测试设定 */
        set_app_set_test(buff_wifi.rxd[WIFI_ORDER_ADDR]);
        break;

    case DPID_APP_SHOW_DATA: /* 0xC7，app展示数据设定 */
        set_app_show_data(buff_wifi.rxd[WIFI_ORDER_ADDR]);
        break;

    case DPID_LOW_HEAT: /* 0xB4，低温制热设定 */
        set_lowheat(buff_wifi.rxd[WIFI_ORDER_ADDR]);
        break;

    case DPID_MANUAL_DEFROST: /* 0xB5，手动化霜设定 */
        if (buff_wifi.rxd[WIFI_ORDER_ADDR] == ON)
        {
            set_manual_defrost(ON);
        }
        break;

    default:
        break;
    }

    if (f_upd_power == FALSE)
    {
        BuzzerAct(BUZZER100ms);
    }
    else
    {
        BuzzerAct(BUZZER200ms);
    }

    f_eepRomChg = 1;
    CtrlEndTime = 0;

#ifdef CTRL_485
    update_wireCtrl = 2;
#endif
}

void Comm_wifi_data_up(void) /* 上行数据处理 */
{
    U8 tempA;

    if (f_Defrosting != f_wifi_defrost)
    {
        f_wifi_defrost = f_Defrosting;

        f_upd_manual_defrost = ON;
    }

    if (f_upd_power == ON)
    {
        CLR(f_upd_power);

        Comm_wifi_data_up_bool(DPID_POWER, b_power.set);
    }
    else if (f_upd_mode == ON)
    {
        CLR(f_upd_mode);

        Comm_wifi_data_up_enum(DPID_MODE, e_mode.set);

        SET(f_upd_tarTemp_C);
        SET(f_upd_tarTemp_F);
        SET(f_upd_tarTemp_U);
        SET(f_upd_fanStep);
        SET(f_upd_verSw);
        SET(f_upd_horSw);
    }
    else if (f_upd_tarTemp_C == ON)
    {
        CLR(f_upd_tarTemp_C);

        if (b_eco.set == ON)
        {
            Comm_wifi_data_up_value(DPID_TARTEMP_C, l_tarTemp_C.run / 10);
        }
        else if (b_lowheat.set == ON)
        {
            Comm_wifi_data_up_value(DPID_TARTEMP_C, l_tarTemp_C.run / 10);
        }
        else
        {
            Comm_wifi_data_up_value(DPID_TARTEMP_C, l_tarTemp_C.set[e_mode.set] / 10);
        }
    }
    else if (f_upd_tarTemp_F == ON)
    {
        CLR(f_upd_tarTemp_F);

        if (b_eco.set == ON && b_eco.run == ON)
        {
            Comm_wifi_data_up_value(DPID_TARTEMP_F, 800 / 10);
        }
        else if (b_lowheat.set == ON && b_lowheat.run == ON)
        {
            Comm_wifi_data_up_value(DPID_TARTEMP_F, 460 / 10);
        }
        else
        {
            Comm_wifi_data_up_value(DPID_TARTEMP_F, l_tarTemp_F.set[e_mode.set] / 10);
        }
    }
    else if (f_upd_tarTemp_U == ON)
    {
        CLR(f_upd_tarTemp_U);

        Comm_wifi_data_up_enum(DPID_TEMPUNIT, b_tempFah.set);
    }
    else if (f_upd_fanStep == ON)
    {
        CLR(f_upd_fanStep);

        if (e_fanStep.set[e_mode.set] == 0)
        {
            tempA = 0x00;
        }
        else if (e_fanStep.set[e_mode.set] < 5)
        {
            tempA = 0x01;
        }
        else if (e_fanStep.set[e_mode.set] < 20)
        {
            tempA = 0x02;
        }
        else if (e_fanStep.set[e_mode.set] < 40)
        {
            tempA = 0x03;
        }
        else if (e_fanStep.set[e_mode.set] < 60)
        {
            tempA = 0x04;
        }
        else if (e_fanStep.set[e_mode.set] < 80)
        {
            tempA = 0x05;
        }
        else if (e_fanStep.set[e_mode.set] < 95)
        {
            tempA = 0x06;
        }
        else
        {
            tempA = 0x7;
        }

        Comm_wifi_data_up_enum(DPID_FANLEVEL, tempA);
    }
    else if (f_upd_verSw == ON)
    {
        CLR(f_upd_verSw);

        Comm_wifi_data_up_enum(DPID_VERSWING, e_verSw.set[e_mode.set]);
    }
    else if (f_upd_horSw == ON)
    {
        CLR(f_upd_horSw);

        Comm_wifi_data_up_enum(DPID_HORSWING, e_horSw.set[e_mode.set]);
    }
    else if (f_upd_eco == ON)
    {
        CLR(f_upd_eco);

        Comm_wifi_data_up_bool(DPID_ECO, b_eco.set);
    }
    else if (f_upd_dryer == ON)
    {
        CLR(f_upd_dryer);

        Comm_wifi_data_up_bool(DPID_DRYER, b_dryer.set);
    }
    else if (f_upd_heater == ON)
    {
        CLR(f_upd_heater);

        Comm_wifi_data_up_bool(DPID_HEATER, b_heater.set);
    }
    else if (f_upd_light == ON)
    {
        CLR(f_upd_light);

        Comm_wifi_data_up_bool(DPID_LIGHT, b_light.set);
    }
    else if (f_upd_sleep == ON)
    {
        CLR(f_upd_sleep);

        Comm_wifi_data_up_bool(DPID_SLEEP, b_sleep.set);
    }
    else if (f_upd_health == ON)
    {
        CLR(f_upd_health);

        Comm_wifi_data_up_bool(DPID_HEALTH, b_health.set);
    }
    else if (f_upd_childlock == ON)
    {
        CLR(f_upd_childlock);

        Comm_wifi_data_up_bool(DPID_CHILDLOCK, b_childlock.set);
    }
    else if (f_upd_buzzer == ON)
    {
        CLR(f_upd_buzzer);

        Comm_wifi_data_up_bool(DPID_BUZZER, b_buzzer.set);
    }
    else if (f_upd_humidifier == ON)
    {
        CLR(f_upd_humidifier);

        Comm_wifi_data_up_bool(DPID_HUMIDIFIER, b_humidifier.set);
    }
    else if (f_upd_tarhum == ON)
    {
        CLR(f_upd_tarhum);

        Comm_wifi_data_up_value(DPID_TARHUM, l_tarHum.set / 10);
    }
    /*
    else if (f_upd_timer == ON)
    {
        CLR(f_upd_timer);

        if (b_power.set == OFF)
        {
            Comm_wifi_data_up_enum(DPID_TIMER, (l_timerOn.set + 30) / 60);
        }
        else
        {
            Comm_wifi_data_up_enum(DPID_TIMER, (l_timerOff.set + 30) / 60);
        }
    }
    else if (f_upd_timer_left == ON)
    {
        CLR(f_upd_timer_left);

        if (b_power.set == OFF)
        {
            Comm_wifi_data_up_value(DPID_TIMER_LEFT, l_timerOn.set);
        }
        else
        {
            Comm_wifi_data_up_value(DPID_TIMER_LEFT, l_timerOff.set);
        }
    }
    */
    else if (f_upd_low_heat == ON)
    {
        CLR(f_upd_low_heat);

        Comm_wifi_data_up_bool(DPID_LOW_HEAT, b_lowheat.set);
    }
    else if (f_upd_manual_defrost == ON)
    {
        CLR(f_upd_manual_defrost);

        if (f_Defrosting)
        {
            Comm_wifi_data_up_bool(DPID_MANUAL_DEFROST, f_Defrosting);
        }
        else
        {
            Comm_wifi_data_up_bool(DPID_MANUAL_DEFROST, b_manual_defrost.set);
        }
    }
    else if (f_upd_set_cap_test == ON)
    {
        CLR(f_upd_set_cap_test);

        Comm_wifi_data_up_enum(DPID_SET_CAP_TEST, CapTestStat);
    }
    else if (f_upd_set_exp_step == ON)
    {
        CLR(f_upd_set_exp_step);

        Comm_wifi_data_up_value(DPID_SET_EXP_STEP, SetExpValue);
    }
    else if (f_upd_set_exh_temp == ON)
    {
        CLR(f_upd_set_exh_temp);

        Comm_wifi_data_up_value(DPID_SET_EXH_TEMP, SetExhValue);
    }
    else if (f_upd_set_comp_freq == ON)
    {
        CLR(f_upd_set_comp_freq);

        Comm_wifi_data_up_value(DPID_SET_COMP_FREQ, SetFreValue);
    }
    else if (f_upd_set_out_fan == ON)
    {
        CLR(f_upd_set_out_fan);

        Comm_wifi_data_up_value(DPID_SET_OUT_FAN, SetOutFanValue);
    }
    else if (f_upd_set_in_fan == ON)
    {
        CLR(f_upd_set_in_fan);

        Comm_wifi_data_up_value(DPID_SET_IN_FAN, SetInFanValue);
    }
    else if (f_upd_set_sw_step == ON)
    {
        CLR(f_upd_set_sw_step);

        Comm_wifi_data_up_value(DPID_SET_SW_STEP, SetSwStepValue);
    }
    else if (f_upd_set_correct == ON)
    {
        CLR(f_upd_set_correct);

        Comm_wifi_data_up_enum(DPID_SET_CORRECT, setCorreck);
    }
    else if (f_upd_app_set_test == ON)
    {
        CLR(f_upd_app_set_test);

        Comm_wifi_data_up_bool(DPID_APP_SET_TEST, setAppSetTest);
    }
    else if (f_upd_app_show_data == ON)
    {
        CLR(f_upd_app_show_data);

        Comm_wifi_data_up_bool(DPID_APP_SHOW_DATA, setAPPShowData);
    }
    else if (f_upd_comp_freq == ON) // && b_power.run == ON) /* 压缩机频率定时上报标志 */
    {
        CLR(f_upd_comp_freq);

        Comm_wifi_data_up_value(DPID_COMP_FREQ, parameter_out.comp_freq);
    }
    else if (f_upd_ofan_speed == ON) // && b_power.run == ON) /* 外风机转速定时上报标志 */
    {
        CLR(f_upd_ofan_speed);

        Comm_wifi_data_up_value(DPID_OFAN_SPEED, parameter_out.ofan_speed * 10);
    }
    else if (f_upd_eve_step == ON) // && b_power.run == ON) /* 电子膨胀阀开度定时上报标志 */
    {
        CLR(f_upd_eve_step);

        Comm_wifi_data_up_value(DPID_EVE_STEP, parameter_out.eve_value);
    }
    else if (f_upd_ac_voltage == ON) // && b_power.run == ON) /* 外机交流电压定时上报标志 */
    {
        CLR(f_upd_ac_voltage);

        Comm_wifi_data_up_value(DPID_AC_VOLTAGE, parameter_out.ac_voltage);
    }
    else if (f_upd_dc_voltage == ON) // && b_power.run == ON) /* 直流母线电压定时上报标志 */
    {
        CLR(f_upd_dc_voltage);

        Comm_wifi_data_up_value(DPID_DC_VOLTAGE, parameter_out.dc_voltage);
    }
    else if (f_upd_ac_current == ON) // && b_power.run == ON) /* 外机交流电流定时上报标志 */
    {
        CLR(f_upd_ac_current);

        Comm_wifi_data_up_value(DPID_AC_CURRENT, parameter_out.ac_current);
    }
    else if (f_upd_dc_current == ON) // && b_power.run == ON) /* 压缩机相电流定时上报标志 */
    {
        CLR(f_upd_dc_current);

        Comm_wifi_data_up_value(DPID_DC_CURRENT, parameter_out.dc_current);
    }
    else if (f_upd_ifan_set_rpm == ON) // && e_fanStep.run != FANSTOP) /* 内风机设定转速定时上报标志 */
    {
        CLR(f_upd_ifan_set_rpm);

        Comm_wifi_data_up_value(DPID_IFAN_SET_RPM, speedSetValue);
    }
    else if (f_upd_ifan_run_rpm == ON) // && e_fanStep.run != FANSTOP) /* 内风机实际转速定时上报标志 */
    {
        CLR(f_upd_ifan_run_rpm);

        Comm_wifi_data_up_value(DPID_IFAN_RUN_RPM, speedNowValue1);
    }
    else if (f_upd_out_temp == ON) // && b_power.run == ON) /* 外环温度定时上报标志 */
    {
        CLR(f_upd_out_temp);

        Comm_wifi_data_up_value(DPID_OUT_TEMP, (S32)temp.outdoor - 400);
    }
    else if (f_upd_out_tube_temp == ON) // && b_power.run == ON) /* 外管温度定时上报标志 */
    {
        CLR(f_upd_out_tube_temp);

        Comm_wifi_data_up_value(DPID_OUT_TUBE_TEMP, (S32)temp.out_tube - 400);
    }
    else if (f_upd_out_exha_temp == ON) // && b_power.run == ON) /* 排气温度定时上报标志 */
    {
        CLR(f_upd_out_exha_temp);

        Comm_wifi_data_up_value(DPID_OUT_EXHA_TEMP, (S32)temp.out_exha - 400);
    }
    else if (f_upd_out_ipm_temp == ON) // && b_power.run == ON) /* 压缩机ipm温度定时上报标志 */
    {
        CLR(f_upd_out_ipm_temp);

        Comm_wifi_data_up_value(DPID_OUT_IPM_TEMP, (S32)temp.comp_ipm - 400);
    }
    else if (f_upd_in_temp_c == ON) /* 内环摄氏度定时上报标志 */
    {
        CLR(f_upd_in_temp_c);

        Comm_wifi_data_up_value(DPID_IN_TEMP_C, (S32)temp.room - 400);
    }
    else if (f_upd_in_tube_temp == ON) // && b_power.run == ON) /* 内管温度定时上报标志 */
    {
        CLR(f_upd_in_tube_temp);

        Comm_wifi_data_up_value(DPID_IN_TUBE_TEMP, (S32)temp.tube - 400);
    }
    else if (f_upd_in_humi == ON) /* 内环湿度定时上报标志 */
    {
        CLR(f_upd_in_humi);

        Comm_wifi_data_up_value(DPID_IN_HUMI, htu20.hum);
    }
    else if (f_upd_in_temp_f == ON) /* 内环华氏度定时上报标志 */
    {
        CLR(f_upd_in_temp_f);

        Comm_wifi_data_up_value(DPID_IN_TEMP_F, (S32)(temp.room - 400) / 10 * 18 + 320);
    }
    else
    {
        if (f_upd_error_code == ON || Comm_wifi_error_check() == 1)
        {
            CLR(f_upd_error_code);

            Comm_wifi_data_up_fault(DPID_FAULT);
        }
    }
}

U8 Comm_wifi_error_check(void)
{
    U8 i, temp_fault[4];

    for (i = 0; i < 4; i++)
    {
        temp_fault[i] = fault_flag[i].byte;
    }

    f_fault1 = f_JumpErrQT;
    // f_fault2 = f_ZeroErr || f_ZeroErrQT;
    f_fault2 = f_waterErr;
    f_fault3 = f_MotorErr;
    f_fault4 = f_CommErr;
    f_fault5 = f_RoomErr || f_RoomErrQT;
    f_fault6 = f_TubeErr || f_TubeErrQT;
    f_fault7 = f_OutErr_E2;
    f_fault8 = f_OutErr_E3;

    f_fault9 = f_OutErr_E4;
    f_fault10 = f_OutErr_E5;
    f_fault11 = f_OutErr_E8;
    f_fault12 = f_htu20_Err;

    f_fault21 = f_OutErr_L6;
    f_fault23 = f_OutErr_L9;
    f_fault24 = f_OutErr_U0;

    f_fault26 = f_OutErr_U6 || f_OutErr_U8;
    f_fault29 = f_comm485Err; // f_OutErr_Ud;
    f_fault30 = f_OutErr_UE;

    if ((CallDispMin > 0 || CompOffSec >= 200)) // error display of 2
    {
        f_fault13 = f_OutErr_H0;
        f_fault14 = f_OutErr_H1;
        f_fault15 = f_OutErr_H2;
        f_fault16 = f_OutErr_H3;

        f_fault17 = f_OutErr_H7;
        f_fault18 = f_OutErr_H8;
        f_fault19 = f_OutErr_L0 || f_OutErr_L1 || f_OutErr_L2 || f_OutErr_L4;
        f_fault20 = f_OutErr_L5;

        if (f_skflg == 0)
            f_fault22 = f_OutErr_L8_0 || f_OutErr_L8_1 || f_OutErr_L8_2 || f_OutErr_L8_3 || f_OutErr_L8_4;
        else
            f_fault22 = f_OutErr_L8_0 || f_OutErr_L8_1 || f_OutErr_L8_2 || f_OutErr_L9 || f_OutErr_L8_4;

        f_fault25 = f_OutErr_U3 || f_OutErr_U4 || f_OutErr_U5;

        f_fault27 = f_OutErr_U7;
        f_fault28 = f_OutErr_U9;
    }

    for (i = 0; i < 4; i++)
    {
        if (temp_fault[i] != fault_flag[i].byte)
        {
            return 1;
        }
    }

    return 0;
}

void Comm_wifi_data_up_bool(U8 dpid, U8 data)
{
    memset(buff_wifi.txd, 0, sizeof(buff_wifi.txd));

    buff_wifi.txd[WIFI_HEADER0_ADDR] = WIFI_HEADER0_BYTE;
    buff_wifi.txd[WIFI_HEADER1_ADDR] = WIFI_HEADER1_BYTE;

    buff_wifi.txd[WIFI_VERSION_ADDR] = WIFI_VERSION_BYTE;
    buff_wifi.txd[WIFI_COMMAND_ADDR] = 0x07;

    buff_wifi.txd[WIFI_LENGTH0_ADDR] = 0x00;
    buff_wifi.txd[WIFI_LENGTH1_ADDR] = 0x05;

    buff_wifi.txd[WIFI_DPID_ADDR] = dpid;
    buff_wifi.txd[WIFI_TYPE_ADDR] = 0x01;

    buff_wifi.txd[8] = 0x00;
    buff_wifi.txd[9] = 0x01;

    buff_wifi.txd[10] = data;

    buff_wifi.txd[11] = get_check_sum(buff_wifi.txd, 11);

    Comm_wifi_Tx(buff_wifi.txd, 12);
}

void Comm_wifi_data_up_enum(U8 dpid, U8 data)
{
    memset(buff_wifi.txd, 0, sizeof(buff_wifi.txd));

    buff_wifi.txd[WIFI_HEADER0_ADDR] = WIFI_HEADER0_BYTE;
    buff_wifi.txd[WIFI_HEADER1_ADDR] = WIFI_HEADER1_BYTE;

    buff_wifi.txd[WIFI_VERSION_ADDR] = WIFI_VERSION_BYTE;
    buff_wifi.txd[WIFI_COMMAND_ADDR] = 0x07;

    buff_wifi.txd[WIFI_LENGTH0_ADDR] = 0x00;
    buff_wifi.txd[WIFI_LENGTH1_ADDR] = 0x05;

    buff_wifi.txd[WIFI_DPID_ADDR] = dpid;
    buff_wifi.txd[WIFI_TYPE_ADDR] = 0x04;

    buff_wifi.txd[8] = 0x00;
    buff_wifi.txd[9] = 0x01;

    buff_wifi.txd[10] = data;

    buff_wifi.txd[11] = get_check_sum(buff_wifi.txd, 11);

    Comm_wifi_Tx(buff_wifi.txd, 12);
}

void Comm_wifi_data_up_value(U8 dpid, S32 data)
{
    memset(buff_wifi.txd, 0, sizeof(buff_wifi.txd));

    buff_wifi.txd[WIFI_HEADER0_ADDR] = WIFI_HEADER0_BYTE;
    buff_wifi.txd[WIFI_HEADER1_ADDR] = WIFI_HEADER1_BYTE;

    buff_wifi.txd[WIFI_VERSION_ADDR] = WIFI_VERSION_BYTE;
    buff_wifi.txd[WIFI_COMMAND_ADDR] = 0x07;

    buff_wifi.txd[WIFI_LENGTH0_ADDR] = 0x00;
    buff_wifi.txd[WIFI_LENGTH1_ADDR] = 0x08;

    buff_wifi.txd[WIFI_DPID_ADDR] = dpid;
    buff_wifi.txd[WIFI_TYPE_ADDR] = 0x02;

    buff_wifi.txd[8] = 0x00;
    buff_wifi.txd[9] = 0x04;

    buff_wifi.txd[10] = (data & 0xFF000000) >> 24;
    buff_wifi.txd[11] = (data & 0x00FF0000) >> 16;
    buff_wifi.txd[12] = (data & 0x0000FF00) >> 8;
    buff_wifi.txd[13] = data & 0x000000FF;

    buff_wifi.txd[14] = get_check_sum(buff_wifi.txd, 14);

    Comm_wifi_Tx(buff_wifi.txd, 15);
}

void Comm_wifi_data_up_fault(U8 dpid)
{
    memset(buff_wifi.txd, 0, sizeof(buff_wifi.txd));

    buff_wifi.txd[WIFI_HEADER0_ADDR] = WIFI_HEADER0_BYTE;
    buff_wifi.txd[WIFI_HEADER1_ADDR] = WIFI_HEADER1_BYTE;

    buff_wifi.txd[WIFI_VERSION_ADDR] = WIFI_VERSION_BYTE;
    buff_wifi.txd[WIFI_COMMAND_ADDR] = 0x07;

    buff_wifi.txd[WIFI_LENGTH0_ADDR] = 0x00;
    buff_wifi.txd[WIFI_LENGTH1_ADDR] = 0x08;

    buff_wifi.txd[WIFI_DPID_ADDR] = dpid;
    buff_wifi.txd[WIFI_TYPE_ADDR] = 0x05;

    buff_wifi.txd[8] = 0x00;
    buff_wifi.txd[9] = 0x04;

    buff_wifi.txd[10] = fault_flag[3].byte;
    buff_wifi.txd[11] = fault_flag[2].byte;
    buff_wifi.txd[12] = fault_flag[1].byte;
    buff_wifi.txd[13] = fault_flag[0].byte;

    buff_wifi.txd[14] = get_check_sum(buff_wifi.txd, 14);

    Comm_wifi_Tx(buff_wifi.txd, 15);
}

void commwifiTime(void) /* called in systime.c per 500us */
{
    if (timer_wifi.rxd > 0)
    {
        timer_wifi.rxd--;
    }
}
